import graphs.CycleException;

import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import poset.ExplicitRedundancyException;
import poset.Item;
import poset.Ordering;
import poset.RectangularPosetSpace;
import poset.SphericalItem;
import poset.SphericalOrdering;
import poset.SphericalPoset;
import poset.SphericalPosetSpace;
import poset.SphericalUser;
import poset.User;
import poset.Poset;
import poset.ReflexiveOrderingException;


public class PosetSpaceSettlingTesterMain {

	public static void main(String[] args) {
		JFrame frame = new JFrame("Poset Predictor");
		frame.setBounds(100,100,700,500);
		//PosetSpaceViewer psv = createPosetSpaceViewer();
		PosetSpaceViewer psv = createPosetSpaceViewer();
		frame.setContentPane(psv);
		frame.setVisible(true);
		psv.start();
		
	}
/*
	private static RectangularPosetSpaceViewer createPosetSpaceViewer(){
		ArrayList<Item> items = new ArrayList<Item>();
		for(int i=0;i<4;i++){
			items.add(new Item("Item"+i));
		}
		ArrayList<User> users = new ArrayList<User>();
		for(int i=0;i<3;i++){
			User user = new User("User"+i);
			Poset poset = new Poset();
			for(int j=0;j<5;j++){
				if(Math.random()>0){
					try {
						Ordering o = new Ordering( items.get((int)(items.size()*Math.random())), items.get((int)(items.size()*Math.random())) );
						poset.addOrdering(o);
					} catch (ReflexiveOrderingException e) {
						// Do nothing, just doesn't add the illegal ordering
						System.out.println("Reflexive");
					} catch (CycleException e) {
						// Do nothing, just doesn't add the illegal ordering
						System.out.println("Loop");
					} catch (ExplicitRedundancyException e) {
						// Do nothing, just doesn't add the illegal ordering
						System.out.println("Explicit Redundancy");
					}
				}
			}
			user.addPoset("like",poset);
			System.out.println(user);
			users.add(user);
		}
		RectangularPosetSpace ps = new RectangularPosetSpace("like", users, 2);
		RectangularPosetSpaceViewer psv = new RectangularPosetSpaceViewer(ps);
		return psv;
	}
	*/
	private static PosetSpaceViewer createPosetSpaceViewer(){
		/*
		ArrayList<SphericalItem> items = new ArrayList<SphericalItem>();
		for(int i=0;i<20;i++){
			items.add(new SphericalItem("Item"+i));
		}
		ArrayList<SphericalUser> users = new ArrayList<SphericalUser>();
		SphericalUser user1 = new SphericalUser("User1");
		SphericalUser user2 = new SphericalUser("User2");
		SphericalPoset poset1 = new SphericalPoset();
		SphericalPoset poset2 = new SphericalPoset();
		for(int j=0;j<19;j++){
			try {
				SphericalOrdering o1 = new SphericalOrdering( items.get(j), items.get(j+1) );
				poset1.addOrdering(o1);
				SphericalOrdering o2 = new SphericalOrdering( items.get(j+1), items.get(j) );
				poset2.addOrdering(o2);
			} catch (ReflexiveOrderingException e) {
				// Do nothing, just doesn't add the illegal ordering
				System.out.println("Reflexive");
			} catch (CycleException e) {
				// Do nothing, just doesn't add the illegal ordering
				System.out.println("Loop");
			} catch (ExplicitRedundancyException e) {
				// Do nothing, just doesn't add the illegal ordering
				System.out.println("Explicit Redundancy");
			}
		}
		user1.addPoset("like",poset1);
		user2.addPoset("like",poset2);
		users.add(user1);
		users.add(user2);
		*/
		
		ArrayList<Item> items = new ArrayList<Item>();
		for(int i=0;i<15;i++){
			items.add(new Item("Item"+i));
		}
		ArrayList<User> users = new ArrayList<User>();
		for(int i=0;i<15;i++){
			User user = new User("User"+i);
			Poset poset = new Poset();
			for(int j=0;j<15;j++){
				if(Math.random()>0){
					try {
						Ordering o = new Ordering( items.get((int)(items.size()*Math.random())), items.get((int)(items.size()*Math.random())) );
						poset.addOrdering(o);
					} catch (ReflexiveOrderingException e) {
						// Do nothing, just doesn't add the illegal ordering
						System.out.println("Reflexive");
					} catch (CycleException e) {
						// Do nothing, just doesn't add the illegal ordering
						System.out.println("Loop");
					} catch (ExplicitRedundancyException e) {
						// Do nothing, just doesn't add the illegal ordering
						System.out.println("Explicit Redundancy");
					}
				}
			}
			user.addPoset("like",poset);
			System.out.println(user);
			users.add(user);
		}
		
		SphericalPosetSpace ps = new SphericalPosetSpace("like", users, 2);
		SphericalPosetSpaceViewer psv = new SphericalPosetSpaceViewer(ps);
		//RectangularPosetSpace ps = new RectangularPosetSpace("like", users, 2);
		//RectangularPosetSpaceViewer psv = new RectangularPosetSpaceViewer(ps);
		return psv;
	}

}
